package com.rongc.client.freight.business.carrier.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.exception.RegexException;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.DateUtil;
import com.rongc.client.core.utils.SPUtil;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.model.DicsBean;
import com.rongc.client.freight.base.model.ProvinceList;
import com.rongc.client.freight.base.model.ProvinceTimeList;
import com.rongc.client.freight.base.model.TipTemp;
import com.rongc.client.freight.base.view.activity.MapGdListActivity;
import com.rongc.client.freight.base.view.widget.wheelwidget.OptionsPickerView;
import com.rongc.client.freight.base.view.widget.wheelwidget.Time2PickerView;
import com.rongc.client.freight.business.carrier.model.ConveyanceDic;
import com.rongc.client.freight.business.carrier.view.fragment.ConveyanceSupplyFragment;
import com.rongc.client.freight.business.mine.model.CarDic;
import com.rongc.client.freight.business.supply.model.SupplyMyList;
import com.rongc.client.freight.business.supply.request.api.SupplySearchApi;
import com.rongc.client.freight.utils.UtilMethod;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Bruce on 2017/7/7.
 */
public class ConveyanceSupplyListActivity extends BaseActivity implements View.OnClickListener{
    ConveyanceSupplyFragment fragment;

//    Time2PickerView pvOptionsProvince;
    OptionsPickerView pvOptionsProvince;
    Time2PickerView pvOptisonsProvinceCar;

    @Bind(R.id.tv_departure)
    TextView mTvDeparture;
    @Bind(R.id.tv_destination)
    TextView mTvDestination;
    @Bind(R.id.tv_date)
    TextView mTvDate;
    @Bind(R.id.tv_length)
    TextView mTvLength;
    String startLon,startLat,endLon,endLat,carModel,carLength,loadTime;
    int page=1;

    @OnClick({R.id.lyt_filter_departure,R.id.lyt_filter_destination,R.id.lyt_filter_date,R.id.lyt_filter_length})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.lyt_filter_departure:
                ActivityUtils.startActivityForResult(ConveyanceSupplyListActivity.this,MapGdListActivity.class, ConveyanceDic.ResultType.DEPARTURE_TYPE);
                break;
            case R.id.lyt_filter_destination:
                ActivityUtils.startActivityForResult(ConveyanceSupplyListActivity.this,MapGdListActivity.class, ConveyanceDic.ResultType.DESTINATION_TYPE);
                break;
            case R.id.lyt_filter_date:
//                SystemUtil.hideSystemKeyboad(ConveyanceSupplyListActivity.this);
                pvOptionsProvince.setPicker(ProvinceList.getOptions1Items(),ProvinceList.getOptions2Items(),true);
                pvOptionsProvince.setCyclic(false, false, false);
                pvOptionsProvince.setSelectOptions(0, 0, 0);
                pvOptionsProvince.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3) {
                        String tx =ProvinceList.getOptions1Items().get(options1).getName() + " "
                                + ProvinceList.getOptions2Items().get(options1).get(option2).getName();
                        mTvDate.setText(DateUtil.getStringByFormat(tx,DateUtil.dateFormatMD));
                        loadTime = tx;

                        doSearch(1);
                    }
                });
                pvOptionsProvince.show();

                break;
            case R.id.lyt_filter_length:
//                ActivityUtils.startActivityForResult(ConveyanceSupplyListActivity.this,CarLengthActivity.class, CarDic.ResultType.Length_TYPE);
//                SystemUtil.hideSystemKeyboad(ConveyanceSupplyListActivity.this);
                pvOptisonsProvinceCar.setPicker(ProvinceTimeList.getOptionsLengthItems()
                        , ProvinceTimeList.getOptionsModelItems());
                pvOptisonsProvinceCar.setCyclic(false, false, false);
                pvOptisonsProvinceCar.setSelectOptions(0, 0, 0);
                pvOptisonsProvinceCar.setOnoptionsSelectListener(new Time2PickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3) {
                        carModel = ProvinceTimeList.getOptionsModelItems().get(option2).getName();
                        carLength = ProvinceTimeList.getOptionsLengthItems().get(options1).getName();
                        mTvLength.setText(ProvinceTimeList.getOptionsModelItems().get(option2).getDescription());

                        doSearch(1);
                    }
                });
                pvOptisonsProvinceCar.show();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            switch (requestCode){
                case  CarDic.ResultType.Length_TYPE:
                    DicsBean.DicBean dataStr = (DicsBean.DicBean) data.getSerializableExtra("data");
                    carLength = dataStr.getKey();
                    break;
                case ConveyanceDic.ResultType.DEPARTURE_TYPE:
                    TipTemp result = (TipTemp) data.getSerializableExtra("data");
                    startLon = result.getLongitude();
                    startLat = result.getLatitude();
                    mTvDeparture.setText(result.getName());
                    break;
                case ConveyanceDic.ResultType.DESTINATION_TYPE:
                    result = (TipTemp) data.getSerializableExtra("data");
                    endLon = result.getLongitude();
                    endLat = result.getLatitude();
                    mTvDestination.setText(result.getName());
                    break;
            }
            doSearch(1);
        }
    }


    public void doSearch(int page){
        this.page = page;
        try{
            prepare();
            SupplySearchApi p = new SupplySearchApi(new SupplySearchApi.SupplySearchParams(
                    startLon,startLat,endLon,endLat,
                    carLength,carModel,loadTime,page+""),
                    respWorkersListener, errorListener);
            RequestManager.getInstance().call(p);
        }catch (RegexException e){
            ActivityUtils.toast(e.getMessage());
        }
    }

    @Override
    public void callbackFun1(Bundle arg) {
        page = arg.getInt("page",page);
        doSearch(page);
    }

    void prepare() throws RegexException {
        /*if (!RegexUtil.checkNotNull(startLon)) {
            throw new RegexException("请选择出发地");
        } else if (!RegexUtil.checkNotNull(startLat)) {
            throw new RegexException("请选择出发地");
        }*/
    }

    Response.Listener<SupplyMyList> respWorkersListener = new Response.Listener<SupplyMyList>() {

        @Override
        public void onResponse(SupplyMyList response) {
            if (HandleCode.requestSuccess()) {
//                ActivityUtils.toast(HandleCode.headerMessage);
                fragment.setDatas(response.getList(),page);
            } else {
                UtilMethod.dismissProgressDialog(ConveyanceSupplyListActivity.this);
            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            UtilMethod.dismissProgressDialog(ConveyanceSupplyListActivity.this);
        }
    };

    @Override
    public void initView() {
        startLon = SPUtil.getString("lon");
        startLat = SPUtil.getString("lat");
        initToolbar(R.string.conveyance_supply_title);
        fragment = ConveyanceSupplyFragment.getInstance();
        ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),fragment,R.id.contentFrame);
        doSearch(1);
        pvOptionsProvince = new OptionsPickerView(this);
        pvOptisonsProvinceCar = new Time2PickerView(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_supply_car;
    }
}
